package shy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import shy.bean.Adoptive;
import shy.mapper.AdoptiveMapper;

import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/adoptive")
public class AdoptiveController {
    @Resource
    private AdoptiveMapper mapper;

    @RequestMapping("/find")
    public PageInfo<Adoptive> find(@RequestParam(defaultValue = "1") int page) {
        //引用分页操作
        PageHelper.startPage(page, 10);
        //全查询
        List<Adoptive> list = mapper.findAllAdoptive();
        // 拆箱装箱
        //返回分页信息：当前数据，总页数，上一页，下一页等；
        PageInfo<Adoptive> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //    删除方法
    @RequestMapping("/delete")
    public int delete(int id) {
        return mapper.deleteById(id);
    }

    //    条件搜索
    @RequestMapping("/find_name")
    public PageInfo<Adoptive> findAdoptiveByAdoCodeAndAdoName(String adoCode, String adoName, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        PageHelper.startPage(page, size);
        List<Adoptive> list = mapper.findAdoptiveByCodeAndName("%" + adoCode + "%", "%" + adoName + "%");
        PageInfo<Adoptive> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @RequestMapping("/save")
    public int save(Adoptive adoptive) {
        return mapper.saveAdoptive(adoptive);
    }

    @RequestMapping("/update")
    public int update(Adoptive adoptive) {
        System.out.println(adoptive);
        return mapper.updateAdoptive(adoptive);
    }
}
